package com.LittleHan.crm.workbench.service;

import com.LittleHan.crm.exception.ActivityCreateException;
import com.LittleHan.crm.exception.ActivityDeleteException;
import com.LittleHan.crm.exception.ActivityRemarkException;
import com.LittleHan.crm.exception.ActivityUpdateException;
import com.LittleHan.crm.settings.dao.UserDao;
import com.LittleHan.crm.settings.entity.User;
import com.LittleHan.crm.utils.DateTimeUtil;
import com.LittleHan.crm.vo.PaginationVo;
import com.LittleHan.crm.workbench.dao.ActivityDao;
import com.LittleHan.crm.workbench.dao.ActivityRemarkDao;
import com.LittleHan.crm.workbench.entity.Activity;
import com.LittleHan.crm.workbench.entity.ActivityRemark;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//@Service(value="activityServiceImpl")
public class ActivityServiceImpl implements ActivityService{
    /*@Autowired
            @Qualifier(value = "activityDao")*/
    ActivityDao activityDao;
    ActivityRemarkDao activityRemarkDao;
    UserDao userDao;
    public ActivityDao getActivityDao() {
        return activityDao;
    }
    public void setActivityDao(ActivityDao activityDao) {
        this.activityDao = activityDao;
    }
    public ActivityRemarkDao getActivityRemarkDao() {
        return activityRemarkDao;
    }
    public void setActivityRemarkDao(ActivityRemarkDao activityRemarkDao) {
        this.activityRemarkDao = activityRemarkDao;
    }
    public UserDao getUserDao() {
        return userDao;
    }
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    @Override
    public int activityNewPreserve(Activity activity) throws ActivityCreateException {
        int sum=activityDao.insertIntoActivity(activity);
        if(sum==0){
            throw new ActivityCreateException("创建(添加)失败error");
        }
        return sum;
    }

    public PaginationVo getTotalAndList(int pageNo,int pageSize,String searchName,String searchOwner,String searchStartDate,String searchEndDate){
        List<Activity> list=activityDao.getDataList(pageNo,pageSize,searchName,searchOwner,searchStartDate,searchEndDate);
        int num=activityDao.getTotal(pageNo,pageSize,searchName,searchOwner,searchStartDate,searchEndDate);
        PaginationVo<Activity> paginationVo=new PaginationVo<>();
        //paginationVo.setTotal(t);
        paginationVo.setDataList(list);
        paginationVo.setTotal(num);
        return paginationVo;
    }

    public int activityDelete(String []id) throws ActivityDeleteException{
        int count=activityRemarkDao.countActivityRemark(id);
        int delete=activityRemarkDao.deleteActivityRemark(id);
        if(delete!=count){
            throw new ActivityDeleteException("删除失败");
        }
        int n=activityDao.deleteActivity(id);
        if(n!=id.length){
            throw new ActivityDeleteException("删除失败");
        }
        return n;
    }

    public Map<String,Object> activityUpdate(String id){
        Activity activity=activityDao.getId(id);
        List<User> userList=userDao.activityGetUserList();
        Map<String,Object> map=new HashMap<>();
        map.put("userList",userList);
        map.put("activity",activity);
        return map;
    }

    @Override
    public int activityUpdatePoint(Activity activity) throws ActivityUpdateException{
        int n=activityDao.updateActivity(activity);
        if(n==0){
            throw new ActivityUpdateException("更新失败");
        }
        return 0;
    }

    @Override
    public Map<String, Object> getDetailInfList(String id) {
        Activity activity=activityDao.getDetailInf(id);
        Map<String,Object> map=new HashMap<>();
        map.put("activity",activity);
        return map;
    }

    @Override
    public List<ActivityRemark> activityListRemark(String id){
        List<ActivityRemark> list=activityRemarkDao.getActivityListRemark(id);
        return list;
    }

    @Override
    public int remarkDelete(String id) throws ActivityRemarkException {
        int num=activityRemarkDao.deleteRemark(id);
        //int num=0;
        if(num==0){
            throw new ActivityRemarkException("删除失败");
        }
        return num;
    }

    @Override
    public int remarkAdd(ActivityRemark activityRemark) {
        int num=activityRemarkDao.addRemark(activityRemark);
        return num;
    }

    @Override
    public int remarkUpdate(ActivityRemark activityRemark) {
        int num=activityRemarkDao.updateRemark(activityRemark);
        return num;
    }
}
